package com.purong.purong.widget;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.purong.purong.R;
import com.purong.purong.adapter.TextChoseDialogAdapter;
import com.purong.purong.bean.TextItemBean;
import com.purong.purong.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zengwenjie on 2017/11/23.
 */
public class TextChoseDialog extends Dialog {
    private RecyclerView recyclerView;
    private TextChoseDialogAdapter mAdapter;
    private List<TextItemBean> itemList=new ArrayList<>();
    private Activity mActivity;
    private OnTextChoseListener mListener;
    public TextChoseDialog(Activity context) {
        super(context, R.style.DialogTransparent);
        this.mActivity=context;
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window .setGravity( Gravity.BOTTOM);
        window.setWindowAnimations(R.style.dialogWindowAnim);
    }
    public void setOnTextChoseListener(OnTextChoseListener listener){
        this.mListener=listener;
    }
    public void setData(List<TextItemBean> itemList){
        this.itemList.clear();
        this.itemList.addAll(itemList);
    }

    @Override
    public void show() {
        if (itemList.size()>0){
            super.show();
            mAdapter.notifyDataSetChanged();
        }else {
            if (mActivity!=null)
            Toast.makeText(mActivity,"暂无数据",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.dialog_textitem_chose_list, null);
        setContentView(inflate);
        findViewById(R.id.main_lay).setLayoutParams(new FrameLayout.LayoutParams(CommonUtil.getScreenWidth(mActivity),CommonUtil.getScreenHeight(mActivity)/4*3));
        findViewById(R.id.empty_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
//        RelativeLayout recyclerLay=(RelativeLayout)  inflate.findViewById(R.id.recycler_lay);
//        recyclerLay.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,CommonUtil.getScreenHeight(mActivity)/4*1));
        recyclerView=(RecyclerView) inflate.findViewById(R.id.recycler_view);
        mAdapter=new TextChoseDialogAdapter(mActivity);
        mAdapter.setData(itemList);;
        mAdapter.setOnTextClickListener(new TextChoseDialogAdapter.OnTextClickListener() {
            @Override
            public void onClick(int position) {
                if (mListener!=null){
                    mListener.onChose(position,itemList.get(position));
                }
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new FullyLinearLayoutManager(mActivity,LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(mAdapter);



    }

    public interface OnTextChoseListener{
        void onChose(int position, TextItemBean item);
    }
}
